package com.upeoe.shiro.chapter4;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.AesCipherService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.Sha384Hash;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.SimpleByteSource;
import org.junit.Assert;
import org.junit.Test;

import java.security.Key;
import java.util.Arrays;

/**
 * @author ven_hxz
 * @create 2016-07-03 下午4:31
 */
public class Main {
    @Test
    public void testBase64() {
        String str = "shiro";
        String base64Encoded = Base64.encodeToString(str.getBytes());
        String str2 = Base64.decodeToString(base64Encoded);

        Assert.assertEquals(str, str2);
    }

    @Test
    public void testHex() {
        String str = "shiro";
        String hexEncoded = Hex.encodeToString(str.getBytes());
        String str2 = new String(Hex.decode(hexEncoded.getBytes()));
        System.out.println(hexEncoded);

        Assert.assertEquals(str, str2);
    }

    @Test
    public void testCodecSupport() {
        String str = "shiro";
        byte[] bytes = CodecSupport.toBytes(str, "utf-8");
        String str2 = CodecSupport.toString(bytes);
        System.out.println(Arrays.toString(bytes));
        System.out.println(str2);
    }

    @Test
    public void testMD5() {
        String str = "shiro";
        String salt = "shiro";
        String md5Str = new Md5Hash(str, salt).toString();
        String md5Base64 = new Md5Hash(str, salt).toBase64();
        String md5Hex = new Md5Hash(str, salt).toHex();

        String md5Str2 = new Md5Hash(str, salt, 2).toString();//Md5(Md5(str))

        System.out.println(md5Str);
        System.out.println(md5Base64);
        System.out.println(md5Hex);
        System.out.println(md5Str2);
    }

    @Test
    public void testSHA256() {
        String str = "shiro";
        String salt = "shiro";
        String sha256Str = new Sha256Hash(str, salt).toString();
        String sha256Base64 = new Sha256Hash(str, salt).toBase64();
        String sha256Hex = new Sha256Hash(str, salt).toHex();

        System.out.println(sha256Str);
        System.out.println(sha256Base64);
        System.out.println(sha256Hex);

        System.out.println(new Sha1Hash(str, salt));
        System.out.println(new Sha384Hash(str, salt));
        System.out.println(new Sha512Hash(str, salt));
    }

    @Test
    public void test() {
        String str = "123";
        System.out.println(new Sha512Hash(str));
    }

    @Test
    public void testSimpleHash() {
        String str = "shiro";
        String salt = "shiro";
        //内部使用MessageDigest
        String simpleHash = new SimpleHash("SHA-1", str, salt).toString();

        System.out.println(simpleHash);
    }

    @Test
    public void testRandom() {
        //生成随机数
        SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
        randomNumberGenerator.setSeed("shiro".getBytes());
        System.out.println(randomNumberGenerator.nextBytes().toHex());
    }

    @Test
    public void testHashService() {
        DefaultHashService hashService = new DefaultHashService();//默认算法SHA-512
        hashService.setHashAlgorithmName("SHA-512");
        hashService.setPrivateSalt(new SimpleByteSource("shiro"));//私盐,默认无
        hashService.setGeneratePublicSalt(true);//在用户没有传入公盐的时候是否生成公盐,默认false
        hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//用于生成公盐
        hashService.setHashIterations(1);//生成Hash值得迭代次数

        HashRequest request = new HashRequest.Builder()
                .setAlgorithmName("SHA-512")
                .setSource(ByteSource.Util.bytes("123"))
                .setSalt(ByteSource.Util.bytes("aa123"))
                .setIterations(1)
                .build();
        String hex = hashService.computeHash(request).toHex();
        System.out.println(hex);
    }

    @Test
    public void testAesCipherService() {
        AesCipherService aesCipherService = new AesCipherService();
        aesCipherService.setKeySize(128);//设置key长度
        Key key = aesCipherService.generateNewKey();

        String str = "shiro";
        //加密
        String encrptText = aesCipherService.encrypt(str.getBytes(), key.getEncoded()).toHex();
        //解密
        String str2 = new String(aesCipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());

        Assert.assertEquals(str, str2);
    }
}
